# ------------------------------------------------------------------------------
#
# Copyright (c) 2011 - The OWE Team
#
# This file is part of OWE.
#
# OWE is a free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; version
# version 2.1 of the License.
#
# OWE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with OWE. If not, see <http://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------------------------

add_definitions(-DTEST_DATA="${CMAKE_CURRENT_SOURCE_DIR}/data")

# ------------------------------------------------------------------------------
# Unit tests helper library
# ------------------------------------------------------------------------------
add_library(oweutests SHARED ${CMAKE_CURRENT_SOURCE_DIR}/utests.cpp)
target_link_libraries(oweutests owe)

# ------------------------------------------------------------------------------
# TUT, the self contained, header based and portable unit test framework
# ------------------------------------------------------------------------------
include_directories(${CMAKE_SOURCE_DIR}/src/3rdparty/tut)

# ------------------------------------------------------------------------------
# Unit tests list, one file per class
# ------------------------------------------------------------------------------
set(TESTS
    tst_configuration_authorelement
    tst_configuration_configparser
    tst_configuration_configurationdocument
    tst_configuration_contentelement
    tst_configuration_descriptionelement
    tst_configuration_element
    tst_configuration_featureelement
    tst_configuration_iconelement
    tst_configuration_licenseelement
    tst_configuration_nameelement
    tst_configuration_paramelement
    tst_configuration_preferenceelement
    tst_configuration_textelement
    tst_configuration_widgetelement
    tst_interfaces_widgetinterface
    tst_utils_platform
    tst_utils_ustring
)

foreach(TEST ${TESTS})
    add_executable(${TEST} ${CMAKE_CURRENT_SOURCE_DIR}/${TEST}.cpp)
    target_link_libraries(${TEST} oweutests owe)
    add_test(${TEST} ${CMAKE_CURRENT_BINARY_DIR}/${TEST})
endforeach(TEST)

add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS ${TESTS})

# ------------------------------------------------------------------------------
# Valgrind support when available (check utests for leaks and heap violation)
# ------------------------------------------------------------------------------
find_program(VALGRIND "valgrind")

if (VALGRIND)
    add_custom_target(check_valgrind)
    add_dependencies(check_valgrind check)

    # Platform initialization is leaked by default
    list(REMOVE_ITEM TESTS "tst_utils_platform")

    foreach(TEST ${TESTS})
        add_custom_command(TARGET check_valgrind POST_BUILD
            COMMAND ${VALGRIND}
            ARGS --quiet --error-exitcode=1 --leak-check=full
            --suppressions=${CMAKE_SOURCE_DIR}/scripts/valgrind.sup ${CMAKE_CURRENT_BINARY_DIR}/${TEST}
        )
    endforeach(TEST)
endif()
